package com.howie.shirojwt.controller;

import com.howie.shirojwt.model.ResultMap;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created with IntelliJ IDEA
 *
 * @Description 游客角色可以访问的页面
 */
@RestController
@RequestMapping("/guest")
public class GuestController {
    private final ResultMap resultMap;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Autowired
    public GuestController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    @GetMapping("/welcome")
    public ResultMap login() {
        return resultMap.success().code(200).message("欢迎访问游客页面！");
    }

    @GetMapping("/getRd")
    public ResultMap getRedis(@RequestParam("key") String key){
    String v =stringRedisTemplate.opsForValue().get(key);
        return resultMap.success().code(200).message(v);
    }

    @Autowired
    CloseableHttpClient httpClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/callA")
    public ResultMap  call() throws IOException {
        HttpGet get = new HttpGet("http://localhost:7771/callService");
        CloseableHttpResponse response = httpClient.execute(get);
        String r1 = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println("Service A httpClient:" + r1);



        String r2 = restTemplate.getForObject("http://localhost:7772/callService", String.class);


        System.out.println("Service B httpClient:" + r2);
        return resultMap.success().code(200).message(r1+r2);
    }



}
